trimesh interface

Convert ObjMesh to trimesh’s mesh class.

In this notebook, we define functions to convert our ObjMesh class to and from trimesh (https://trimesh.org/) which is a python library for triangle meshes. I generally prefer to use igl, but maybe trimesh has some feature you want.

Note: trimesh represents triangular meshes only, and its way of representing UV information is not ideal. It is not recommended to edit mesh topology in trimesh if the UV mapping has already been defined.


source

convert_to_trimesh

 convert_to_trimesh (mesh:blender_tissue_cartography.mesh.ObjMesh,
                     add_texture_info=None)

*Convert tcmesh.ObjMesh to trimesh.Trimesh

See https://trimesh.org/trimesh.base.html Note: normal information is recalculated. Discards any non-triangle faces.

Texture is saved as a vertex attribute via v_tex_coords_matrix. Note that this discards information since a vertex can have multiple texture coordinates! For this reason, we also add the texture coordinates as a (n_faces, 3, s)-array attribute face_tex. Note: this will not be updated if you remesh etc.*

Type Default Details
mesh ObjMesh
add_texture_info NoneType None Whether to add texture info to the trimesh.Trimesh. If None, texture is added if
available for at least one vertex.
Returns Trimesh
mesh_fname_data = "datasets/registration_example/Drosophila_CAAX-mCherry_mesh_remeshed.obj"
mesh_fname_ref = "datasets/registration_example/Drosophila_reference.obj"
mesh_data = tcmesh.ObjMesh.read_obj(mesh_fname_data)
mesh_ref = tcmesh.ObjMesh.read_obj(mesh_fname_ref)
Warning: readOBJ() ignored non-comment line 4:
  o embryo_rect
trimesh_data = convert_to_trimesh(mesh_data)
np.allclose(mesh_data.vertices, trimesh_data.vertices)
True
trimesh_normals = trimesh_data.vertex_normals
trimesh_normals = (trimesh_normals.T / np.linalg.norm(trimesh_normals, axis=-1)).T

np.einsum('vi,vi->v', mesh_data.normals, trimesh_normals)
array([6.27953731, 6.26481851, 6.27572446, ..., 6.24734426, 6.22183872,
       6.25817202])

source

convert_from_trimesh

 convert_from_trimesh (mesh:trimesh.base.Trimesh,
                       reconstruct_texture_from_faces=True,
                       texture_vertex_decimals=10)

*Convert trimesh mesh to ObjMesh.

Texture vertices can be reconstructed from face attribute face_tex or from vertex attribute vertex_tex_coord_matrix. Reconstruction from face texture can accommodate multiple texture coordinates per vertex (e.g. for UV maps with seams).*

Type Default Details
mesh Trimesh
reconstruct_texture_from_faces bool True Whether to reconstruct texture information from per-face data (True), or
per-vertex data (False)
texture_vertex_decimals int 10 Texture vertices are rounded to texture_vertex_decimals decimals.
Returns ObjMesh
trimesh_ref = convert_to_trimesh(mesh_ref)
mesh_seams = tcmesh.ObjMesh.read_obj("datasets/drosophila_example/Drosophila_CAAX-mCherry_mesh_uv.obj")
trimesh_seams = convert_to_trimesh(mesh_seams,add_texture_info=True)
Warning: readOBJ() ignored non-comment line 4:
  o Drosophila_CAAX-mCherry_mesh_remeshed
Warning: readOBJ() ignored non-comment line 48073:
  l 2534 8160
/home/nikolas/Programs/miniconda3/envs/blender-tissue-cartography/lib/python3.11/site-packages/trimesh/grouping.py:99: RuntimeWarning: invalid value encountered in cast
  stacked = np.column_stack(stacked).round().astype(np.int64)
convert_from_trimesh(trimesh_seams, reconstruct_texture_from_faces=False).texture_vertices.shape, mesh_seams.texture_vertices.shape
((8159, 2), (8288, 2))